// page_two/foodcns/foodcns.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '承诺书',
    currtab: 0,
    canvasHidden: true,
    ratio: 0, // 根据尺寸动态计算 1px换算成多少rpx
    windowWidth: 0, // 可使用窗口宽度
    windowHeight: 0, // 可使用窗口高度
    canvasW: 350, //canvas宽
    canvasH: 574, //canvas高
    aaa: 1,
    qming: '',
    qqqm: false,
    qianming_id: '',
    yearcount:"",

  },
  // 没有签名时打开签名
  qianmi(e) {
    var id = e.currentTarget.dataset.id
    var fromid = e.currentTarget.dataset.fromid
    this.setData({
      qianming_id: id,
      fromid: fromid,
      iaas: true
    })
    this.setData({
      qqqm: !this.data.qqqm
    })
  },

  // 修改签名
  xiugaiqm() {
    this.setData({
      qqqm: !this.data.qqqm,
      isshw: true
    })
    this.chengns()
  },
  goHistory(){
    wx.navigateTo({
      url: '/page_other/historybook/historybook',
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var test = new Date();
    var year = test.getFullYear();

    console.log(options)
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      gao: wx.getMenuButtonBoundingClientRect().top + 40,
      yearcount:year,

    })
  },
  downLoad: function (e) {
    var that = this;
    wx.showLoading({
      title: '正在生成图片',
    })
    // 背景 承诺书
    wx.downloadFile({
      url: that.data.cns,
      success: function (res3) {
        that.setData({
          cnss: res3.tempFilePath
        })
        // 签名
        if (that.data.qming != '') {
          console.log(121111111111)
          wx.downloadFile({
            url: that.data.qming,
            success: function (res3) {
              that.setData({
                qian: res3.tempFilePath
              })
              that.createCanvasShareImage();
            }
          })
        } else {
          console.log(3333333333333)
          that.createCanvasShareImage();
        }
      }
    })
  },
  // 绘制
  createCanvasShareImage() {
    let context = wx.createCanvasContext('shareCanvas' + this.data.aaa);
    let canvasWidthPx = 700 / this.data.ratio;
    var windowWidth = this.data.windowWidth //手机宽
    var windowHeight = this.data.windowHeight //手机搞
    // x轴
    var pagex = 70
    var pagey = 240
    // canvas背景色
    context.fillStyle = '#fff';
    context.fillRect(0, 0, this.data.canvasW, this.data.canvasH);
    // 承诺书
    context.drawImage(this.data.chengnus, 0, 0, this.data.canvasW, this.data.canvasH);
    // 零售
    // context.drawImage(this.data.lingshou, 0, 0, this.data.canvasW, this.data.canvasH);
    // 器械
    // context.drawImage(this.data.qixie, 0, 0, this.data.canvasW, this.data.canvasH);
    // 店铺名
    context.fillStyle = '#000';
    context.setFontSize(16);
    context.fillText(this.data.dianpu, 90, 99);
    // 时间
    context.fillStyle = '#000';
    context.setFontSize(14);
    context.fillText(this.data.shijian, this.data.canvasW - 110, this.data.canvasH - 63);
    //签名
    if (this.data.epsituationImg != '') {
      context.drawImage(this.data.epsituationImg, this.data.canvasW - 100, this.data.canvasH - 130, 90, 50);
    }
    context.draw();
    wx.hideLoading({})
    // this.setData({
    //   xiazai: true
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.getSystemInfo({
      success: res => {
        // console.log(res);
        this.setData({
          windowWidth: res.windowWidth * 1.5,
          windowHeight: res.windowHeight,
        });
        this.setData({
          // 屏幕宽度 375px = 750rpx，1px=2rpx
          // 1px = （750 / 屏幕宽度）rpx；
          // 1rpx = （屏幕宽度 / 750）px;
          ratio: 750 / this.data.windowWidth
        });
      }
    });
  },
  // 签名
  bindtouchstart: function (e) {
    this.data.context.moveTo(e.changedTouches[0].x, e.changedTouches[0].y);
  },
  clear: function () {
    this.data.context.draw();
    this.data.context.setFillStyle('#fff')
    this.data.context.setLineWidth(3)
    this.data.context.fillRect(0, 0, 350, 200)
  },
  bindtouchmove: function (e) {
    // console.log(e.changedTouches)
    this.data.context.lineTo(e.changedTouches[0].x, e.changedTouches[0].y);
    this.data.context.stroke();
    this.data.context.draw(true);
    this.data.context.moveTo(e.changedTouches[0].x, e.changedTouches[0].y);
  },
  export: function () {
    wx.showLoading({
      title: '签字中',
    })
    let that = this;
    that.data.context.fillStyle = '#ffffff'
    that.data.context.draw(true, function () {
      setTimeout(function () {
        wx.canvasToTempFilePath({
          fileType: 'png',
          x: 0,
          y: 0,
          width: 350,
          height: 200,
          destWidth: 250,
          destHeight: 100,
          quality: 1,
          canvasId: 'firstCanvas' + that.data.aaa,
          success(res) {
            console.log(222)
            let {
              tempFilePath
            } = res;
            console.log(tempFilePath)
            wx.uploadFile({
              url: yuming + 'Besall/Upload',
              filePath: tempFilePath,
              name: 'file',
              formData: {

              },
              success(res) {
                console.log(333)
                var data = JSON.parse(res.data)
                var qianming = data.httpUrl + data.imgUrl
                var wuurl = data.imgUrl
                that.setData({
                  qianming_tu: qianming,
                  wuqianming_tu: data.imgUrl,
                })
                that.setData({
                  qqqm: false
                })
                that.queqian()
                // wx.setStorageSync('yqsign', qianming);
                // wx.showToast({
                //   title: '签名成功',
                //   icon: 'none'
                // })
                // that.setData({
                //   qqqm: false,
                // })
                // that.chengns()
                // setTimeout(function () {
                //   that.shengcnuoshu()
                // }, 1000)
              }
            })
          },
          fail(err) {
            console.log(err)
            wx.showToast({
              title: err,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }, 1000)
    })
  },
  // 合成签名的接口
  queqian() {
    this.myRequest(yuming + 'Besall/upload_imgs_compose', {
      shopId: wx.getStorageSync('shopId'),
      id: this.data.qianming_id,
      url_b: this.data.wuqianming_tu
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        var that = this
        var data = res.data
        var qianming = data.httpUrl + data.imgUrl
        var wuurl = data.imgUrl
        if (that.data.fromid == 'shipinanquanchengbuoshu') {
          that.setData({
            wuyi_yiqin: wuurl,
            yi_yiqin: qianming,
          })
          setTimeout(function () {
            that.tijiao()
          }, 1000)
        }
        // if (that.data.fromid == 'yaopinlingshou') {
        //   that.setData({
        //     wuling_yiqin: wuurl,
        //     ling_yiqin: qianming,
        //   })
        //   setTimeout(function () {
        //     that.tijiao()
        //   }, 1000)
        // }
        // if (that.data.fromid == 'yiliaoqixie') {
        //   that.setData({
        //     wuqixie_yiqin: wuurl,
        //     qixie_yiqin: qianming,
        //   })
        //   setTimeout(function () {
        //     that.tijiao()
        //   }, 1000)
        // }

      }
    })
  },
  //放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 获取承诺书
  chengns() {
    var that = this
    this.myRequest(yuming + 'Besall/backFoodSafetyBgimg', {
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      var data = res.data.data
      var das = res.data
      console.log(das)
      // data[0].tagers = '流通'
      data[0].isqian = das.foodsafetyImg
      // data[1].tagers = '器械'
      // data[1].isqian = das.apparatusImg
      that.setData({
        yi_yiqin: das.foodsafetyImg, //防疫 合成之后的
        // ling_yiqin: das.retailImg, //零售
        // qixie_yiqin: das.apparatusImg, //器械
        ulist: data
      })

      that.setData({
        cns: res.data.data,
        shijian: res.data.day,
        dianpu: res.data.shopName,
        qming: wx.getStorageSync('yqsign'),

      })

      // 签名
      const context = wx.createCanvasContext('firstCanvas' + this.data.aaa);
      context.setFillStyle('#fff')
      context.fillRect(0, 0, 350, 200)
      context.setLineWidth(3)
      this.setData({
        context
      });
      context.draw()
    })
  },
  // 确定生成承诺书
  // shengcheng() {
  //   var that = this
  //   that.shengcnuoshu()

  // },
  // 生成承诺书
  shengcnuoshu() {
    wx.showLoading({
      title: '合成中...',
    })
    var that = this
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'shareCanvas' + that.data.aaa,
        success: (res) => {
          console.log('canvasToTempFilePath： ', res);
          that.data.aaa++;
          that.setData({
            aaa: that.data.aaa
          })
          var url = res.tempFilePath
          wx.uploadFile({
            url: yuming + 'Besall/Upload',
            filePath: url,
            name: 'file',
            formData: {

            },
            success(res) {
              console.log(333)
              var data = JSON.parse(res.data)
              var img = data.imgUrl
              console.log(img)
              that.setData({
                foodsafetyImg: img,
              })
              wx.hideLoading({
                success: (res) => {
                  that.tijiao()
                },
              })
            }
          })

        }
      })
    }, 1000)
  },
  tijiao() {
    var that = this
    var yi_yiqin = ''
    var ling_yiqin = ''
    var qixie_yiqin = ''
    var data = {}
    if (that.data.fromid == 'shipinanquanchengbuoshu') {
      data = {
        shopId: wx.getStorageSync('shopId'),
        foodsafetyImg: that.data.wuyi_yiqin
      }
      this.myRequest(yuming + 'shop/shopSaveBossBg', data, "POST").then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          setTimeout(function () {
            that.data.aaa++;
            that.setData({
              aaa: that.data.aaa,
              ling_yiqin: '',
              fromid: '',
              qianming_id: '',
              qianming_tu: '',
              qixie_yiqin: '',
              wuqianming_tu: ''
            })
            that.chengns()
          }, 1000)

        }
      })
    }
    // if (that.data.fromid == 'yaopinlingshou') {
    //   data = {
    //     shopId: wx.getStorageSync('shopId'),
    //     retailImg: that.data.wuling_yiqin
    //   }
    //   this.myRequest(yuming + 'shop/shopSaveBossBg', data, "POST").then(res => {
    //     wx.showToast({
    //       title: res.data.msg,
    //       icon: 'none'
    //     })
    //     if (res.data.code == 200) {
    //       setTimeout(function () {
    //         that.data.aaa++;
    //         that.setData({
    //           aaa: that.data.aaa,
    //           ling_yiqin: '',
    //           fromid: '',
    //           qianming_id: '',
    //           qianming_tu: '',
    //           qixie_yiqin: '',
    //           wuqianming_tu: ''
    //         })
    //         that.chengns()
    //       }, 1000)

    //     }
    //   })
    // }
    // if (that.data.fromid == 'yiliaoqixie') {
    //   data = {
    //     shopId: wx.getStorageSync('shopId'),
    //     apparatusImg: that.data.wuqixie_yiqin
    //   }
    //   this.myRequest(yuming + 'shop/shopSaveBossBg', data, "POST").then(res => {
    //     wx.showToast({
    //       title: res.data.msg,
    //       icon: 'none'
    //     })
    //     if (res.data.code == 200) {
    //       setTimeout(function () {
    //         that.data.aaa++;
    //         that.setData({
    //           aaa: that.data.aaa,
    //           ling_yiqin: '',
    //           fromid: '',
    //           qianming_id: '',
    //           qianming_tu: '',
    //           qixie_yiqin: '',
    //           wuqianming_tu: ''
    //         })
    //         that.chengns()
    //       }, 1000)

    //     }
    //   })
    // }
  },
  // 重新签名
  chongxin() {
    var that = this
    this.myRequest(yuming + 'shop/shopSaveBossBg', {
      shopId: wx.getStorageSync('shopId'),
      foodsafetyImg: ''
    }, "POST").then(res => {
      wx.setStorageSync('spsign', '')
      that.chengns()
      // that.qianmi()
      setTimeout(function () {
        that.setData({
          qqqm: true
        })
      }, 1000)
      // that.qianmi()
      // that.setData({
      //   qqqm:true
      // })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.chengns()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})